package com.cxx.purchasecharge.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cxx.purchasecharge.core.model.persistence.Contact;
import com.cxx.purchasecharge.core.model.persistence.Customer;
import com.cxx.purchasecharge.core.model.persistence.Goods;
import com.cxx.purchasecharge.core.model.persistence.GoodsStorage;
import com.cxx.purchasecharge.core.model.persistence.Order;
import com.cxx.purchasecharge.core.model.persistence.Payment;
import com.cxx.purchasecharge.core.model.persistence.User;
import com.cxx.purchasecharge.dal.CustomerDao;
import com.cxx.purchasecharge.dal.ContactDao;
import com.cxx.purchasecharge.dal.GoodsStorageDao;
import com.cxx.purchasecharge.dal.PaymentDao;
import com.cxx.purchasecharge.dal.UserDao;
import com.cxx.purchasecharge.service.AuditLogService;
import com.cxx.purchasecharge.service.CoreService;
import com.cxx.purchasecharge.service.PersistenceService;
import com.cxx.purchasecharge.service.exception.PcServiceException;

@Repository ("auditLogService")
public class AuditLogServiceImpl implements AuditLogService
{
    @Autowired
    private CoreService coreService;
    @Autowired
    private CustomerDao baseCustomerDao;
    @Autowired
    private ContactDao contactDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private GoodsStorageDao storageDao;
    @Autowired
    private PaymentDao paymentDao;
    @Autowired
    private PersistenceService persistenceService;

    @Override
    public Customer addCustomer (Customer customer, String operator) throws PcServiceException
    {
        return baseCustomerDao.save (customer);
    }

    @Override
    public Customer updateCustomer (Customer customer, String operator) throws PcServiceException
    {
        return baseCustomerDao.save (customer);
    }

    @Override
    public void deleteCustomer (Customer customer, String operator) throws PcServiceException
    {
        baseCustomerDao.delete (customer);
    }

    @Override
    public Contact addContact (Contact contact, String operator) throws PcServiceException
    {
        return contactDao.save (contact);
    }

    @Override
    public Contact updateContact (Contact contact, String operator) throws PcServiceException
    {
        return contactDao.save (contact);
    }

    @Override
    public void deleteContact (Contact contact, String operator) throws PcServiceException
    {
        contactDao.delete (contact);
    }

    @Override
    public User addUser (User user, String operator) throws PcServiceException
    {
        return userDao.save (user);
    }

    @Override
    public User updateUser (User user, String operator) throws PcServiceException
    {
        return userDao.save (user);
    }

    @Override
    public void deleteUser (User user, String operator) throws PcServiceException
    {
        userDao.delete (user);
    }

    @Override
    public GoodsStorage addStorage (GoodsStorage storage, String operator) throws PcServiceException
    {
        return storageDao.save (storage);
    }

    @Override
    public GoodsStorage updateStorage (GoodsStorage storage, String operator) throws PcServiceException
    {
        return storageDao.save (storage);
    }

    @Override
    public void deleteStorage (GoodsStorage storage, String operator) throws PcServiceException
    {
        storageDao.delete (storage);
    }

    @Override
    public Order addOrder (Order order, String operator) throws PcServiceException
    {
        return coreService.saveOrder (order);
    }

    @Override
    public Order updateOrder (Order order, String operator) throws PcServiceException
    {
        throw new PcServiceException ("Not implemented yet");
    }

    @Override
    public void deleteOrder (Order order, String operator) throws PcServiceException
    {
        coreService.deleteOrder (order);
    }

    @Override
    public Payment addPayment (Payment payment, String operator) throws PcServiceException
    {
        Customer customer = baseCustomerDao.findOne (payment.getCustomer ().getId ());
        payment.setAllUnPaid (customer.getDealMoney () - payment.getPaid ());
        Payment p = paymentDao.save (payment);
        customer.setDealMoney (customer.getDealMoney () - p.getPaid ());
        this.updateCustomerPay (customer, operator);
        return p;
    }

    @Override
    public Payment updatePayment (Payment payment, String operator) throws PcServiceException
    {
        return paymentDao.save (payment);
    }

    @Override
    public void deletePayment (Payment payment, String operator) throws PcServiceException
    {
        paymentDao.delete (payment);
    }

    @Override
    public Goods addGoods (Goods goods, String operator) throws PcServiceException
    {
        return persistenceService.addGoods (goods);
    }

    @Override
    public Goods updateGoods (Goods goods, String operator) throws PcServiceException
    {
        return persistenceService.updateGoods (goods);
    }

    @Override
    public void deleteGoods (Goods goods, String operator) throws PcServiceException
    {
        persistenceService.deleteGoods (goods);
    }

    @Override
    public void login (String userId, List <String> authorities) throws PcServiceException
    {
        // TODO
    }

    @Override
    public void logout (String userId) throws PcServiceException
    {
        // TODO
    }

    @Override
    public void updateCustomerPay (Customer customer, String operator) throws PcServiceException
    {
        baseCustomerDao.updateDealMoney (customer.getDealMoney (), customer.getId ());
    }

}
